import { Wsserver } from './../../index';
import { Global } from './../../Global/index';
import { WsConnection } from 'tsrpc';
import { tests } from './../../shared/types/tests';
import { users } from './../../shared/types/user';
import { ObjectId } from "mongodb"
import { MsgStudentStatus } from '../../shared/protocols/AMessages/Test/MsgStudentStatus';

/*
 * @Date: 2023-02-12 17:02:20
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-02-24 21:55:55
 * @FilePath: \A19\backend\src\utils\Classes\Test.ts
 */
export class Test {
    static tests = new Array<Test>
    // static connLen = 1 //4
    
    _id:ObjectId //the test _id in mongo
    testName:string
    // testStatus: 'before' | 'testing' | 'after'
    startTime: Date
    endTime : Date

    // students: Array<{
    //     student:users
    //     connection:WsConnection
    // }>
    students: Map<string,{
        student:users
        connection:WsConnection
    }>
    // connectionArrays:Map<string,Array<ObjectId>>
    
    teacher: {
        teacher:users
        connection:WsConnection
    } | undefined

    constructor(test:tests) {
        this._id = test._id
        this.testName = test.name
        
        this.startTime = test.startTime
        this.endTime = test.endTime

        this.students = new Map<string,{
            student:users
            connection:WsConnection
        }>()
        // this.connectionArrays = new Map<string,Array<ObjectId>>()
        this.teacher = undefined 
        
        Test.tests.push(this)
    }

    //学生加入考试
    static async joinTest(stu:{student:ObjectId,testId:ObjectId},connection:WsConnection):Promise<Test | 'unexist' | 'sunexist' | undefined>{
        // console.log(Test.tests);
        const test = Test.tests.find(test => test._id.equals(stu.testId))
        const student = await Global.collection('users').findOne({_id:stu.student})

        //考试不存在 || 学生不存在
        if(test === undefined) return 'unexist'
        if(student === null) return 'sunexist'

        test.students.set(connection.id,{
            student,
            connection
        })
        console.log("currentStudents: ",test.students.size," teacher: ",test.teacher?.teacher.name);

        //new student joined test
        //给监考老师发送信息
        if(test.teacher === undefined) return undefined
        else{
            // test.teacher.connection.sendMsg('AMessages/Test/StudentStatus',{
            //     students:[...test.students.values()].map(student => student.student)
            // })
            test.teacher.connection.sendMsg('AMessages/Test/ConnectInfo',{
                info:'connected',
                student
            })
            return test
        }
    }

    static async teacherJoin(tea:{teacher:ObjectId,testId:ObjectId},connection:WsConnection){
        const test = Test.tests.find(test => test._id.equals(tea.testId))
        if(test === undefined) {
            //考试不存在
            console.log("考试不存在");
            connection.sendMsg('AMessages/Test/ConnectFailed',{
                message:'unexist'
            })
            return
        }

        const teacher = await Global.collection('users').findOne({
            _id:tea.teacher
        })
        if(teacher === null) return

        test.teacher = {
            teacher,
            connection
        }
        console.log("teacher joined");
        
        //教师进入监考 服务器发送目前考场学生信息
        connection.sendMsg('AMessages/Test/StudentStatus',{
            students:[...test.students.values()].map(student => student.student)
        })

        //通知学生客户端 教师上线 开启rtc
        Wsserver.broadcastMsg('AMessages/Test/TeacherStatus',{
            status:'connected'
        },[...test.students.values()].map(student=>student.connection))
    }

    getTarget(origin:ObjectId):ObjectId{
        return this.teacher?.teacher._id as ObjectId
        // const connectionArray = [...this.connectionArrays.values()].find(connectionArray =>{
        //     return connectionArray.length < Test.connLen
        // })
        // if(connectionArray){
        //     const last = connectionArray.at(-1) as ObjectId
        //     connectionArray.push(origin)

        //     console.log(this.connectionArrays);
        //     // return this.teacher?.teacher._id as ObjectId
        //     console.log("last: ",last);
            
        //     return last
        // }
        // else{
        //     const newArray = new Array<ObjectId>()
        //     newArray.push(origin)
        //     this.connectionArrays.set(origin.toString(),newArray)

        //     console.log(this.connectionArrays);
        //     return this.teacher?.teacher._id as ObjectId
        // }
        
    }
}
